package com.wsz.logistic.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsz.logistic.bean.*;
import com.wsz.logistic.common.form.OrderForm;
import com.wsz.logistic.common.util.R;
import com.wsz.logistic.mapper.*;
import com.wsz.logistic.service.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProvinceMapper provinceMapper;
    @Autowired
    private CityMapper cityMapper;
    @Autowired
    private DistrictMapper districtMapper;
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private WarehouseMapper warehouseMapper;
    @Autowired
    private OrderStatusMapper orderStatusMapper;

    @Override
    public R getPage(int page, int limit, Order order) {
        PageHelper.startPage(page, limit);
        List<Order> orders = orderMapper.getAll(order);
        List<OrderForm> orderForms = new ArrayList<>();
        for (Order o : orders) {
            OrderForm orderForm = new OrderForm();
            BeanUtils.copyProperties(o, orderForm);
            orderForm.setType(typeMapper.selectByPrimaryKey(o.getType()).getName());
            Province sprovince = provinceMapper.selectByPrimaryKey(o.getSenderProvince());
            City scity = cityMapper.selectByPrimaryKey(o.getSenderCity());
            District sdistrict = districtMapper.selectByPrimaryKey(o.getSenderDistrict());
            orderForm.setFullSenderAddress(sprovince.getName() + "省" + scity.getName() + "市" + sdistrict.getName() + "区" + o.getSenderAddress());
            Province rprovince = provinceMapper.selectByPrimaryKey(o.getReceiverProvince());
            City rcity = cityMapper.selectByPrimaryKey(o.getReceiverCity());
            District rdistrict = districtMapper.selectByPrimaryKey(o.getReceiverDistrict());
            orderForm.setFullReceiverAddress(rprovince.getName() + "省" + rcity.getName() + "市" + rdistrict.getName() + "区" + o.getReceiverAddress());
            orderForms.add(orderForm);
        }
        PageInfo<OrderForm> pageInfo = new PageInfo<OrderForm>(orderForms);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void saveOrUpdate(Order order) {
        if (order.getOrderId() == null) {
            orderMapper.insertSelective(order);
            /*OrderStatus orderStatus = new OrderStatus();
            BeanUtils.copyProperties(order, orderStatus);
            orderStatusMapper.insertSelective(orderStatus);*/
            addType(order);
            warehouseSIn(order);
        } else {
            //查询原有订单
            Order o = orderMapper.selectByPrimaryKey(order.getOrderId());
            //若没有传回状态则赋值有状态不变
            if (order.getStatus() == null) {
                order.setStatus(o.getStatus());
            }
            //更改状态时的仓库容量变化
            if ((order.getStatus() == 1 || order.getStatus() == 2) && (o.getStatus() == 4 || o.getStatus() == 5)) {
                warehouseSIn(o);
            } else if ((order.getStatus() == 1 || order.getStatus() == 2) && o.getStatus() == 3) {
                warehouseSIn(o);
                warehouseROut(o);
            } else if ((order.getStatus() == 3 && (o.getStatus() == 1 || o.getStatus() == 2))) {
                warehouseRIn(o);
                warehouseSOut(o);
            } else if (order.getStatus() == 3 && (o.getStatus() == 4 || o.getStatus() == 5)) {
                warehouseSIn(o);
            } else if ((order.getStatus() == 4 || order.getStatus() == 5) && (o.getStatus() == 1 || o.getStatus() == 2)) {
                warehouseSOut(o);
            } else if ((order.getStatus() == 4 || order.getStatus() == 5) && o.getStatus() == 3) {
                warehouseROut(o);
            } else if (order.getStatus() == 3 && order.getReceiverDistrict() != null) {
                warehouseRIn(order);
                warehouseROut(o);
            } else if ((order.getStatus() == 1 || order.getStatus() == 2) && order.getSenderDistrict() != null) {
                warehouseSIn(order);
                warehouseSOut(o);
            }
            //更改分类时的分类数量变化
            if (order.getType() != null && (!order.getType().equals(o.getType()))) {
                addType(order);
                decType(o);
            }
            //完成订单释放分类数量
            if (order.getType() != null && order.getType().equals(5)) {
                decType(o);
            }
            //更新订单
            orderMapper.updateByPrimaryKeySelective(order);
        }
    }

    private void warehouseSIn(Order order) {//发件地入库
        District district = districtMapper.selectByPrimaryKey(order.getSenderDistrict());
        Warehouse warehouse = warehouseMapper.getByName(district.getName());
        warehouse.setCapacity(warehouse.getCapacity() - 1);
        warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    private void warehouseSOut(Order order) {//发件地出库
        District district = districtMapper.selectByPrimaryKey(order.getSenderDistrict());
        Warehouse warehouse = warehouseMapper.getByName(district.getName());
        warehouse.setCapacity(warehouse.getCapacity() + 1);
        warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    private void warehouseRIn(Order order) {//收件地入库
        District district = districtMapper.selectByPrimaryKey(order.getReceiverDistrict());
        Warehouse warehouse = warehouseMapper.getByName(district.getName());
        warehouse.setCapacity(warehouse.getCapacity() - 1);
        warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    private void warehouseROut(Order order) {//收件地出库
        District district = districtMapper.selectByPrimaryKey(order.getReceiverDistrict());
        Warehouse warehouse = warehouseMapper.getByName(district.getName());
        warehouse.setCapacity(warehouse.getCapacity() + 1);
        warehouseMapper.updateByPrimaryKeySelective(warehouse);
    }

    private void addType(Order order) {//增加种类数量
        Type type = typeMapper.selectByPrimaryKey(order.getType());
        type.setNum(type.getNum() + 1);
        typeMapper.updateByPrimaryKeySelective(type);
    }

    private void decType(Order order) {//减少种类数量
        Type type = typeMapper.selectByPrimaryKey(order.getType());
        type.setNum(type.getNum() - 1);
        typeMapper.updateByPrimaryKeySelective(type);
    }

    @Override
    public void deleteById(int orderId) {
        orderMapper.deleteByPrimaryKey(orderId);
    }

    @Override
    public Order getById(int orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public R getProvince() {
        return R.ok(provinceMapper.getAllEA());
    }

    @Override
    public R getCity(int provinceId) {
        return R.ok(cityMapper.getAllByPId(provinceId));
    }

    @Override
    public R getDistrict(int cityId) {
        return R.ok(districtMapper.getAllByCId(cityId));
    }

    @Override
    public R getDistrict() {
        return R.ok(districtMapper.getAll());
    }

    @Override
    public R getTypePage(int page, int limit, Type type) {
        PageHelper.startPage(page, limit);
        List<Type> list = typeMapper.getAll(type);
        PageInfo<Type> pageInfo = new PageInfo<Type>(list);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public void saveOrUpdateType(Type type) {
        if (type.getTypeId() == null) {
            typeMapper.insertSelective(type);
        } else {
            typeMapper.updateByPrimaryKeySelective(type);
        }
    }

    @Override
    public void deleteTypeById(int typeId) {
        typeMapper.deleteByPrimaryKey(typeId);
    }

    @Override
    public Type getTypeById(int typeId) {
        return typeMapper.selectByPrimaryKey(typeId);
    }

    @Override
    public R getTypeAllEA() {
        return R.ok(typeMapper.getAllEA());
    }

    @Override
    public R getMyList(int page, int limit, int deliverierId) {
        PageHelper.startPage(page, limit);
        List<OrderStatus> orderStatuses = orderStatusMapper.getAllOSByDeliverierId(deliverierId);
        List<Order> orders = new ArrayList<>();
        for (OrderStatus orderStatus : orderStatuses) {
            Order order = orderMapper.selectByPrimaryKey(orderStatus.getOrderId());
            orders.add(order);
        }
        List<OrderForm> orderForms = new ArrayList<>();
        for (Order o : orders) {
            OrderForm orderForm = new OrderForm();
            BeanUtils.copyProperties(o, orderForm);
            orderForm.setType(typeMapper.selectByPrimaryKey(o.getType()).getName());
            Province sprovince = provinceMapper.selectByPrimaryKey(o.getSenderProvince());
            City scity = cityMapper.selectByPrimaryKey(o.getSenderCity());
            District sdistrict = districtMapper.selectByPrimaryKey(o.getSenderDistrict());
            orderForm.setFullSenderAddress(sprovince.getName() + "省" + scity.getName() + "市" + sdistrict.getName() + "区" + o.getSenderAddress());
            Province rprovince = provinceMapper.selectByPrimaryKey(o.getReceiverProvince());
            City rcity = cityMapper.selectByPrimaryKey(o.getReceiverCity());
            District rdistrict = districtMapper.selectByPrimaryKey(o.getReceiverDistrict());
            orderForm.setFullReceiverAddress(rprovince.getName() + "省" + rcity.getName() + "市" + rdistrict.getName() + "区" + o.getReceiverAddress());
            orderForms.add(orderForm);
        }
        PageInfo<OrderForm> pageInfo = new PageInfo<OrderForm>(orderForms);
        return R.page(pageInfo.getTotal(), pageInfo.getList());
    }
}
